<?php

namespace app\admin\controller\wechat;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class Menu extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        $post = $this->request->param();
        if ($this->request->isPost()) {

            $data = $post['data'];
            foreach ($data as $info) {
                $info['up_status'] = 2;
                DB::name('wx_menu')->where('id', $info['id'])->update($info);
            }
            $this->success('菜单保存成功');

        } else {
            $wx_button = config('param.wx_button');
            $this->assign('wx_button', $wx_button);
            $url = db::name('wx_menu_url')->select();
            $this->assign('wx_menu_url', $url);
            return view();
        }

    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        unset($post['map']);
        unset($post['page']);
        unset($post['limit']);
        $post['tag_id'] = $post['tag_id'];
        unset($post['tag_id']);
        $data['count'] = Db::name('wx_menu', 'id,name,pid,info,msg,type,status,tag_id,bid')
            ->where($post)
            ->count();
        $data['data'] = Db::name('wx_menu', 'id,name,pid,info,msg,type,status,tag_id,bid')
            ->where($post)
            // ->fetchSql(true)
            ->order('id asc')->select();
        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }

    public function c_wx_menu()
    {
        $post = $this->request->param();
        $list = DB::name('wx_menu')
            ->where('tag_id', 0)
            ->order('bid asc')
            ->select();
        $menu = [];
        $main_menu = 0;
        $wx_button = config('param.wx_button');
        $wx_menu_url = DB::name('wx_menu_url')->select();
        // dump($wx_menu_url);
        //dump($list);
        foreach ($list as $item) {
            // dump(in_array($item['bid'], [10, 20, 30]));
            if (in_array($item['bid'], ['b10', 'b20', 'b30'])) {
                $menu[$main_menu]['name'] = $item['name'];
                $main_menu = $main_menu + 1;
            } else {
                $tmp = [];
                if ($item['name']) {
                    $tmp['name'] = $item['name'];
                    switch ($item['type']) {
                        case 1:
                            $tmp['type'] = 'click';
                            $tmp['key'] = $item['info'];
                            break;
                        case 2:
                            $tmp['type'] = 'view';
                            if ($item['wx_menu_url'] == 0) {
                                $tmp['url'] = $item['info'];
                            } else {
                                $tmp['url'] = $wx_menu_url[$item['wx_menu_url'] - 1]['url'];
                            }
                            break;
                        case 3:
                            $tmp['type'] = 'scancode_push';
                            $tmp['key'] = $item['info'];
                            break;
                        case 4:
                            $tmp['type'] = 'scancode_waitmsg';
                            $tmp['url'] = $item['info'];
                            break;
                        case 5:
                            $tmp['type'] = 'pic_sysphoto';
                            $tmp['key'] = $item['info'];
                            break;
                        case 6:
                            $tmp['type'] = 'pic_photo_or_album';
                            $tmp['key'] = $item['info'];
                            break;
                        case 7:
                            $tmp['type'] = 'pic_weixin';
                            $tmp['key'] = $item['info'];
                            break;
                        case 8:
                            $tmp['type'] = 'location_select';
                            $tmp['key'] = $item['info'];
                            break;
                        case 9:
                            $tmp['type'] = 'media_id';
                            $tmp['media_id'] = $item['info'];
                            break;
                        case 10:
                            $tmp['type'] = 'view_limited';
                            $tmp['media_id'] = $item['info'];
                            break;
                        case 11:
                            $tmp['type'] = 'miniprogram';
                            $tmp['url'] = $item['info'];
                            $tmp['appid'] = $item['msg'];
                            $tmp['pagepath'] = 'pages/index/index';
                            break;
                        default:
                            $tmp['type'] = 'view';
                            $tmp['url'] = '';
                    }
                    $menu[$main_menu - 1]['sub_button'][] = $tmp;
                }
            }
        }
        if ($list[0]['tag_id'] > 0) {
            $new['matchrule']['tag_id'] = $post['tag_id'];
            $url2 = "https://api.weixin.qq.com/cgi-bin/menu/addconditional?access_token=" . cache('access_token');
        } else {
            $url2 = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . cache('access_token');
        }
        // dump($menu);
        $new['button'] = $menu;
        dump($new);
        exit;


    }

}